﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Web.Services;

using LEEAAttendanceControl.Codes;

using System.ServiceModel.DomainServices.Client;

namespace LEEAAttendanceControl.Views
{
    public partial class SpecificHours : Page
    {
        LEEAContext context = new LEEAContext();

        public SpecificHours()
        {
            InitializeComponent();

            Title = ((App)Application.Current).DisplayName + ": Hours";
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ClearEvents();

            HoursBlock.Text = myApp.DisplayName + ": Hours";

            myApp.ChangeEmployeesEvent += RefreshEmployeeDataHandler;

            context.Load(context.GetHoursByEmployeeIDQuery(((App)Application.Current).EmployeeId
                , ((App)Application.Current).PeriodFrom.Date, ((App)Application.Current).PeriodTo.Date)
                , OnHoursLoadCompleted, null);
        }

        private void HoursSummaryDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }

        public void RefreshEmployeeDataHandler(object sender, EventArgs e)
        {
            context.Hours.Clear();

            HoursGrid.ItemsSource = null;

            context.Load(context.GetHoursByEmployeeIDQuery(((App)Application.Current).EmployeeId
                    , ((App)Application.Current).PeriodFrom.Date, ((App)Application.Current).PeriodTo.Date)
                    , OnHoursLoadCompleted, null);
        }

        private void OnHoursLoadCompleted(LoadOperation load)
        {
            HoursGrid.ItemsSource = load.Entities;
        }
    }
}